/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author Aditya-Z
 */
public class KelolaGejala {
     public KelolaGejala() {
        emf = Persistence.createEntityManagerFactory("Dokter_mandiriPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void createGejala(Gejala gejala) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(gejala);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    public Gejala getGejala(String Gejala) {
        Gejala gejala = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(a) FROM Gejala AS a WHERE a.gejala=:gjl");
            q.setParameter("gjl", Gejala);
            gejala = (Gejala) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return gejala;
    }

    public Gejala getNamaPenyakit(String namaPenyakit) {
        Gejala gejala = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(a) FROM Gejala AS a WHERE a.namaPenyakit=:nma");
            q.setParameter("nma", namaPenyakit);
            gejala = (Gejala) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return gejala;
    }
    
     public Gejala getIdGejala(int idGejala) {
        Gejala gejala = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(a) FROM Gejala AS a WHERE a.idGejala=:id");
            q.setParameter("id", idGejala);
            gejala = (Gejala) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return gejala;
    }
    
    public void destroy(Integer idGejala) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Gejala gejala;
            try {
                gejala = em.getReference(Gejala.class, idGejala);
                gejala.getIdGejala();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The gejala with id " + idGejala + " no longer exists.", enfe);
            }
            em.remove(gejala);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    public void edit(Gejala gejala) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            gejala = em.merge(gejala);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = gejala.getIdGejala();
                if (findGejala(id) == null) {
                    throw new NonexistentEntityException("The gejala with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    public Gejala findGejala(Integer idGejala) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Gejala.class, idGejala);
        } finally {
            em.close();
        }
    }

    public List search(String gejala) {
        ArrayList<String> param = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(gejala,",");
        while(st.hasMoreTokens())
        {
            param.add(st.nextToken());
        }
        String query = "";
        for(int i=0; i<param.size(); i++){
            if(i==0) query += "'%" + param.get(i) + "%'";
            else query += " AND o.gejala like '%"+param.get(i) + "%'";
        }
        List result;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT o FROM Gejala AS o WHERE o.gejala LIKE "+query);
            //q.setParameter("gjl", "\"%panas%\" OR \"%mual%\"");
            result = q.getResultList();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return result;
    }

    

    
}
